//相关业务逻辑
$(function () {
  $("#reportDay").val(reportDay);
  report.getUserName();
  report.bind();
  report.getAllSearchWhere();
  report.getAllUser();
  report.getOtherDept();
});

var reportDay = echartUtil.GetDateStr(0);

//时间改变
function dataChange() {
  reportDay = $("#reportDay").val();
}

var report = { //页面总变量
  orderList: [],
  userList: [],
  otherDeptList: [],
  bind: function () {
    $("#coustomId").bind("keyup", function () {
      var orderId = $(this).val();
      var array = new Array();
      for (var i = 0; i < report.orderList.length; i++) {
        if (report.orderList[i].orderId.toUpperCase().indexOf(orderId.toUpperCase()) != -1 || report.orderList[i].companyOrderId.toUpperCase()
          .indexOf(orderId.toUpperCase()) != -1 || report.orderList[i].factoryId.toUpperCase().indexOf(orderId.toUpperCase()) != -1 || report
          .orderList[i].coustomOrderId.toUpperCase().indexOf(orderId.toUpperCase()) != -1 || report.orderList[i].coustomId.toUpperCase()
          .indexOf(orderId.toUpperCase()) != -1) {
          array.push(report.orderList[i]);
          //						html+="<option value='"+report.orderList[i].orderId+"'>"+report.orderList[i].companyOrderId+" &nbsp;&nbsp;&nbsp;&nbsp;"+report.orderList[i].factoryId+" &nbsp;&nbsp;&nbsp;&nbsp;"+report.orderList[i].coustomOrderId+"&nbsp;&nbsp;&nbsp;&nbsp;"+report.orderList[i].coustomId+"</option>";
        }
      }
      if (array.length == 1) {
        $("#orderId").val(array[0].orderId);
        $("#factoryId").text(array[0].factoryId);
        $("#companyOrderId").text(array[0].companyOrderId);
        page.idChange();
      }
    });

    $("#coustomId").bind("change", function () {
      var orderId = $(this).val();
      var ids = orderId.split(" ");
      orderId = ids[0];
      $("#coustomId").val(orderId);
      var array = new Array();
      for (var i = 0; i < report.orderList.length; i++) {
        if (report.orderList[i].orderId.toUpperCase().indexOf(orderId.toUpperCase()) != -1 || report.orderList[i].companyOrderId.toUpperCase()
          .indexOf(orderId.toUpperCase()) != -1 || report.orderList[i].factoryId.toUpperCase().indexOf(orderId.toUpperCase()) != -1 || report
          .orderList[i].coustomOrderId.toUpperCase().indexOf(orderId.toUpperCase()) != -1 || report.orderList[i].coustomId.toUpperCase()
          .indexOf(orderId.toUpperCase()) != -1) {
          array.push(report.orderList[i]);
          //						html+="<option value='"+report.orderList[i].orderId+"'>"+report.orderList[i].companyOrderId+" &nbsp;&nbsp;&nbsp;&nbsp;"+report.orderList[i].factoryId+" &nbsp;&nbsp;&nbsp;&nbsp;"+report.orderList[i].coustomOrderId+"&nbsp;&nbsp;&nbsp;&nbsp;"+report.orderList[i].coustomId+"</option>";
        }
      }
      if (array.length == 1) {
        $("#orderId").val(array[0].orderId);
        $("#factoryId").text(array[0].factoryId);
        $("#companyOrderId").text(array[0].companyOrderId);
        page.idChange();
      }
    });

    $("#reportPageBtn").bind("click", function () {
      var reportPeople = $("#reportPerson").val();
      if (reportPeople == '') {
        layAlert("请输入或选择报工人");
        return false;
      }
      var uu = _.filter(report.userList, function (uu) {
        return uu.userName == reportPeople;
      });
      if (uu.length <= 0) {
        layAlert("该员工不存在");
        return false;
      }
      if (uu.length != 1) {
        layAlert("该姓名重复");
        return false;
      }
      reportPeople = uu[0].id;
      var sendData = page.insertParam();
      console.log(sendData);
      if (sendData == '' || sendData.length <= 0) {
        return false;
      }
      var reportDay = $("#reportDay").val();
      for (var i = 0; i < sendData.length; i++) {
        sendData[i].touchTime = reportDay;
      }
      ajaxUtil.ajaxRuquest('workshop/insertReportByPage', {
        'reportArrayJSONStr': JSON.stringify(sendData),
        'reportUser': reportPeople
      }, function (data) {
        layAlert(data);
        $("#heGe").val("");
        $("#noHeGe").val("");
      });
    });
  },
  getUserName: function () {
    SystemUtil.getUser().then(function (user) {
      $("#userName").text(user.userName);
    });
  },
  getReportPerson: function (order) {
    var userList = [];
    console.log(order);
    if ('多部门 ' == order.frameType) {
      userList = page.userList;
    } else {
      userList = _.filter(report.userList, function (uu) {
        console.log(uu.deptName + "-" + order.deptId);
        return uu.deptName == order.deptId || report.otherDeptList.indexOf(uu.deptName) > -1;
      });
    }
    var html = "";
    for (var i = 0; i < userList.length; i++) {
      html += "<option value='" + userList[i].userName + "'>" + userList[i].userName + "</option>"
    }
    $("#reportPersonList").html(html);
  },
  getAllUser: function () {
    ajaxUtil.ajaxRuquest('user/queryAllUser', null, function (data) {
      if (null == data) {
        data = [];
      }
      report.userList = data;
    });
  },
  getOtherDept: function (order) {
    ajaxUtil.ajaxRuquest('order/getOtherDept', null, function (data) {
      if (null == data) {
        data = [];
      }
      data = _.map(data, "deptId");
      report.otherDeptList = data;
    });
  },
  getAllSearchWhere: function () {
    /**获取所有筛选条件*/
    ajaxUtil.ajaxRuquestNoQuan('companyMaterial/getAllOrderSearchWhere', null, function (data) {
      report.orderList = data;
      report.setView(report.orderList);
    });
  },
  setView: function (data) {
    var html = ""
    for (var i = 0; i < data.length; i++) {
      html += "<option value='" + data[i].orderId + " &nbsp;&nbsp;&nbsp;&nbsp;" + data[i].companyOrderId + " &nbsp;&nbsp;&nbsp;&nbsp;" + data[i]
        .factoryId + " &nbsp;&nbsp;&nbsp;&nbsp;" + data[i].coustomOrderId + "&nbsp;&nbsp;&nbsp;&nbsp;" + data[i].coustomId +
        "' style='display:none'></option>"
    }
    $("#detailList").html(html);
  }
}
